﻿namespace WMS6.BLL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;
    using WMS6.DALFactory;

    public class CartonBLL : IBaseInfo
    {
        private static readonly ICartonDA cartonDA = DataAccess.CreateCartonDA();
        private static readonly ICommonMethodDA commonMethod = DataAccess.CreateCommonMethod();
        private List<string> dataBaseID;
        private PubParasInfo pubParasInfo;

        public CartonBLL(string currentWH)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(currentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(currentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001");
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002");
            }
        }

        public CartonBLL(PubParasInfo tempPubParasInfo)
        {
            this.pubParasInfo = null;
            if (!string.IsNullOrEmpty(tempPubParasInfo.CurrentWH))
            {
                this.dataBaseID = commonMethod.GetDataBaseID(tempPubParasInfo.CurrentWH);
                if (this.dataBaseID == null)
                {
                    PublicMethod.GetInstance().ShowAlertMessage("C001", this.pubParasInfo.UserLanguage);
                }
                else
                {
                    this.pubParasInfo = new PubParasInfo();
                    this.pubParasInfo = tempPubParasInfo;
                }
            }
            else
            {
                PublicMethod.GetInstance().ShowAlertMessage("C002", this.pubParasInfo.UserLanguage);
            }
        }

        public string Delete(List<string> cartonIDs)
        {
            string RetcartonID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                List<CartonInfo> cartonInfos = new List<CartonInfo>();
                CartonInfo cartonInfo = null;
                foreach (string id in cartonIDs)
                {
                    cartonInfo = cartonDA.GetCartonByID(dataBase, tran, id, this.dataBaseID[0]);
                    cartonInfos.Add(cartonInfo);
                }
                AuditDataBLL auditDataBLL = new AuditDataBLL();
                if (cartonDA.DeleteCarton(dataBase, tran, cartonIDs, this.dataBaseID) > 0)
                {
                    for (int i = 0; i < cartonIDs.Count; i++)
                    {
                        auditDataBLL.InsertAuditData<CartonInfo>(dataBase, tran, cartonInfos[i], null, AuditAction.Delete, this.pubParasInfo.UserID, "CARTON", this.dataBaseID);
                        RetcartonID = RetcartonID + cartonIDs[0] + ",";
                    }
                    RetcartonID = RetcartonID.Substring(0, RetcartonID.Length - 1);
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return RetcartonID;
        }

        public DataSet GetAllInfo()
        {
            return cartonDA.GetAllCarton(this.dataBaseID[0]);
        }

        public object GetInfoByID(string cartonID)
        {
            return cartonDA.GetCartonByID(cartonID, this.dataBaseID[0]);
        }

        public DataSet GetInfoByQueryEntity(object obj)
        {
            CartonQueryEntity cartonQuery = obj as CartonQueryEntity;
            List<string> sqlWhere = new List<string>();
            if (!string.IsNullOrEmpty(cartonQuery.CartonID))
            {
                sqlWhere.Add("Carton_ID" + cartonQuery.CartonID);
            }
            if (!string.IsNullOrEmpty(cartonQuery.CartonGroup))
            {
                sqlWhere.Add("Carton_Group" + cartonQuery.CartonGroup);
            }
            if (!string.IsNullOrEmpty(cartonQuery.CartonType))
            {
                sqlWhere.Add("Carton_Type" + cartonQuery.CartonType);
            }
            if (!string.IsNullOrEmpty(cartonQuery.Description))
            {
                sqlWhere.Add("Description" + cartonQuery.Description);
            }
            if (!string.IsNullOrEmpty(cartonQuery.IsActive))
            {
                sqlWhere.Add(" IS_ACTIVE " + cartonQuery.IsActive);
            }
            return cartonDA.GetCartonByQueryList(sqlWhere, cartonQuery, this.dataBaseID[0]);
        }

        public string Insert(object obj)
        {
            string cartonID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                CartonInfo carton = obj as CartonInfo;
                carton.CartonID = commonMethod.GenNextNumStr(dataBase, tran, "CARTON", this.dataBaseID[0]);
                carton.CreatedDate = DateTime.Now;
                carton.UpdatedDate = DateTime.Now;
                carton.CreatedBy = this.pubParasInfo.UserID;
                carton.UpdatedBy = this.pubParasInfo.UserID;
                if (cartonDA.InsertCarton(dataBase, tran, carton, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CartonInfo>(dataBase, tran, null, carton, AuditAction.Insert, this.pubParasInfo.UserID, "CARTON", this.dataBaseID);
                    cartonID = carton.CartonID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return cartonID;
        }

        public string Update(object obj)
        {
            string cartonID = string.Empty;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                CartonInfo carton = obj as CartonInfo;
                carton.UpdatedDate = DateTime.Now;
                CartonInfo oldCartonInfo = cartonDA.GetCartonByID(dataBase, tran, carton.CartonID, this.dataBaseID[0]);
                if (cartonDA.UpdateCarton(dataBase, tran, carton, this.dataBaseID) > 0)
                {
                    new AuditDataBLL().InsertAuditData<CartonInfo>(dataBase, tran, oldCartonInfo, carton, AuditAction.Update, this.pubParasInfo.UserID, "CARTON", this.dataBaseID);
                    cartonID = carton.CartonID;
                }
                tran.Commit();
            }
            catch (Exception ex)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(ex, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return cartonID;
        }
    }
}

